//
//  CategoryViewController.m
//  cookBook
//
//  Created by xalo on 16/1/22.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "CategoryViewController.h"
#import "HeaderReusableView.h"
#import "CategoryHelper.h"
#import "CategoryViewCell.h"
#import "CategoryDetailListViewController.h"

@interface CategoryViewController ()<UISearchBarDelegate>
@property (nonatomic,strong) UISearchBar * searchBar;
@property (nonatomic,strong) UIView * titleView;
@property (nonatomic,strong) CategoryDetailListViewController * categoryVC;
@end

@implementation CategoryViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(searchAction)];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemSearch) target:self action:@selector(searchAction)];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:self.titleView.bounds];
    
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    self.searchBar.delegate = self;
    
    self.titleView = self.navigationItem.titleView;
    
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.searchBar removeFromSuperview];
    self.navigationItem.titleView = self.titleView;
}
-(void)searchAction
{
    if (self.navigationItem.titleView.subviews.count ==0) {
        self.navigationItem.titleView = self.searchBar;
    }else
    {
        [self.searchBar removeFromSuperview];
        self.navigationItem.titleView = self.titleView;
    }
    
}


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSString * searcString = searchBar.text;
    if (![searcString isEqualToString:@""] && searcString.length > 0) {
        self.categoryVC = [[UIStoryboard storyboardWithName:@"CategorySB" bundle:nil] instantiateViewControllerWithIdentifier:@"categoryDetailVC"];
        
        self.categoryVC.titleString = searcString;
        
        self.categoryVC.categoryOrEffect = @"功效";
        
        [self.navigationController pushViewController:self.categoryVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
       return [[CategoryHelper sharedManage] categoryArray].count;
    }else
    {
        return [[CategoryHelper sharedManage] effectArray].count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CategoryViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 0.3;
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    [cell layoutIfNeeded];
    cell.layer.cornerRadius = CGRectGetHeight(cell.frame)/2;
    cell.layer.masksToBounds = YES;
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.8];
    if (indexPath.section == 0) {
        cell.titleLabel.text = [[CategoryHelper sharedManage] categoryArray][indexPath.item];
    }else
        cell.titleLabel.text = [[CategoryHelper sharedManage] effectArray][indexPath.item];
    // Configure the cell
    
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        header.titleLabel.text = @"分类";
    }else
    {
        header.titleLabel.text = @"功效";

    }
    return header;
}
#pragma mark <UICollectionViewDelegate>



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.categoryVC = [[UIStoryboard storyboardWithName:@"CategorySB" bundle:nil] instantiateViewControllerWithIdentifier:@"categoryDetailVC"];
    if (indexPath.section == 0) {
        NSString * string = [[CategoryHelper sharedManage] categoryArray][indexPath.row];
        self.categoryVC.categoryOrEffect = @"分类";
        self.categoryVC.titleString = string;
    }else
    {
        NSString * string = [[CategoryHelper sharedManage] effectArray][indexPath.row];
        self.categoryVC.categoryOrEffect = @"功效";
        self.categoryVC.titleString = string;
    }
    [self.navigationController pushViewController:self.categoryVC animated:YES];
}


/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
